using BCW.Friends;
using UnityEngine;

public class PlayerInfoPanel : MonoBehaviour
{
	public UILabel placeLabel;

	public UILabel leagueLabel;

	public UILabel nameLabel;

	public UILabel stateLabel1;

	public UILabel stateLabel2;

	public UISprite flagTexture;

	public UILabel flagScoreLabel;

	public UISprite buttonFriend;

	private string spritePlus = "Buttons";

	private string spriteSended = "Check";

	private string spriteFriend = "iconMap_Zombie";

	[SerializeField]
	private GameObject addPlayer;

	[SerializeField]
	private GameObject addPlayerInactive;

	[SerializeField]
	private GameObject addedPlayer;

	[SerializeField]
	private GameObject friendPlayer;

	public string idPlayer;

	public UISprite backdround;

	[HideInInspector]
	public int id { get; private set; }

	private void OnEnable()
	{
		FriendsUIGame.CantSendInvite += Active;
	}

	private void OnDisable()
	{
		FriendsUIGame.CantSendInvite -= Active;
	}

	private void OnDestroy()
	{
		FriendsUIGame.CantSendInvite -= Active;
	}

	public void Init(string place, string league, string name, string state1, string state2, Color bgColor, Color textColor, string idPlayer)
	{
		SetLabel(placeLabel, place, textColor);
		SetLabel(leagueLabel, league, textColor);
		SetLabel(nameLabel, name, textColor);
		SetLabel(stateLabel1, state1, textColor);
		SetLabel(stateLabel2, state2, textColor);
		this.idPlayer = idPlayer;
		if (bgColor != Color.magenta)
		{
			backdround.color = bgColor;
		}
		if (flagScoreLabel != null)
		{
			flagScoreLabel.gameObject.SetActive(false);
		}
		if (flagTexture != null)
		{
			flagTexture.gameObject.SetActive(false);
		}
		bool flag = false;
		foreach (string friendsId in FriendsController.instance.FriendsIds)
		{
			if (friendsId == idPlayer)
			{
				ChangeTexture(FriendsButton.Friend);
				flag = true;
			}
		}
		foreach (string listSendedId in FriendsUIGame.instance.listSendedIds)
		{
			if (listSendedId == idPlayer)
			{
				ChangeTexture(FriendsButton.Sended);
				flag = true;
			}
		}
		if (!flag)
		{
			if (FriendsUIGame.instance.CanSendInvite())
			{
				ChangeTexture(FriendsButton.Plus);
			}
			else
			{
				ChangeTexture(FriendsButton.InactiveFriend);
			}
		}
		if (idPlayer == BCWServer.idPlayer)
		{
			addPlayer.SetActive(false);
			addPlayerInactive.SetActive(false);
			addedPlayer.SetActive(false);
			friendPlayer.SetActive(false);
		}
	}

	public void Init(string place, string league, string name, string state1, string state2, Color bgColor, Color textColor, int capturedFlagScore, string idPlayer)
	{
		Init(place, league, name, state1, state2, bgColor, textColor, idPlayer);
		if (capturedFlagScore > 0)
		{
			SetLabel(flagScoreLabel, capturedFlagScore.ToString(), textColor);
			if (flagTexture != null)
			{
				flagTexture.gameObject.SetActive(true);
			}
		}
	}

	private void SetLabel(UILabel label, string text, Color color)
	{
		if (label == null)
		{
			return;
		}
		if (string.IsNullOrEmpty(text))
		{
			label.gameObject.SetActive(false);
			return;
		}
		label.gameObject.SetActive(true);
		if (color != Color.magenta)
		{
			label.color = color;
		}
		label.text = text;
	}

	public void ClickButton()
	{
		FriendsUIGame.instance.ShowWarningMsg(this);
	}

	public void CallBackFriend(ServerResponse response)
	{
		switch (response)
		{
		case ServerResponse.Successful:
			FriendsUIGame.instance.listSendedIds.Add(idPlayer);
			ChangeTexture(FriendsButton.Sended);
			break;
		case ServerResponse.ServerFail:
			FriendsUIGame.instance.curInvitePlayer++;
			if (FriendsUIGame.instance.CanSendInvite())
			{
				ChangeTexture(FriendsButton.Plus);
			}
			else
			{
				ChangeTexture(FriendsButton.InactiveFriend);
			}
			break;
		case ServerResponse.WrongFormat:
			ChangeTexture(FriendsButton.InactiveFriend);
			break;
		}
		FriendsUINotification.instance.AddInQueueWarninMsg(response);
	}

	public void ChangeTexture(FriendsButton friend)
	{
		switch (friend)
		{
		case FriendsButton.Friend:
			addPlayer.SetActive(false);
			addPlayerInactive.SetActive(false);
			addedPlayer.SetActive(false);
			friendPlayer.SetActive(true);
			break;
		case FriendsButton.Plus:
			addPlayer.SetActive(true);
			addPlayerInactive.SetActive(false);
			addedPlayer.SetActive(false);
			friendPlayer.SetActive(false);
			break;
		case FriendsButton.Sended:
			addPlayer.SetActive(false);
			addPlayerInactive.SetActive(false);
			addedPlayer.SetActive(true);
			friendPlayer.SetActive(false);
			break;
		case FriendsButton.InactiveFriend:
			addPlayer.SetActive(false);
			addPlayerInactive.SetActive(true);
			addedPlayer.SetActive(false);
			friendPlayer.SetActive(false);
			break;
		}
	}

	private void Active()
	{
		if (addPlayer.activeSelf)
		{
			ChangeTexture(FriendsButton.InactiveFriend);
		}
	}
}
